package com.zhia.demo.controller;


import com.zhia.demo.entity.NovelEntity;
import com.zhia.demo.service.BrowseHistoryService;
import com.zhia.demo.service.NovelService;
import com.zhia.demo.vo.BookVo;
import com.zhia.demo.vo.ContentVo;
import com.zhia.demo.vo.PageResultVo;
import com.zhia.demo.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import java.util.List;


/**
 * @author DONG
 * @create 2023/10/24 19:02
 */
@RestController
public class NovelController {
    @Autowired
    private NovelService novelService;

    @Autowired
    private BrowseHistoryService browseHistoryService;

    @GetMapping("/books/{cont}")
    public ResultVo searchBook(@PathVariable("cont") String cont, Integer page) {
        ResultVo resultVo = new ResultVo();
        PageResultVo pageResultVo = novelService.PageInfo(cont, page);
        if (pageResultVo.getTotal() != 0) {
            resultVo.setData(pageResultVo);
        } else {
            resultVo.setCode(1L);
        }
        return resultVo;
    }

    @GetMapping("/book/{id}")
    public ResultVo getBookById(@PathVariable("id") String id, @CookieValue(name = "USERSESSION", defaultValue = "") String cookie) {
        ResultVo resultVo = new ResultVo();
        BookVo bookVo = novelService.getBookById(id);
        if (bookVo != null) {
            if (!StringUtils.isEmpty(cookie)) {
                novelService.hotAddById(id);
                bookVo.setHot(bookVo.getHot() + 1);
                Integer chapter = browseHistoryService.userBrowseChapter(id, Integer.parseInt(cookie));
                if (chapter != null)
                    bookVo.setNowChapter(chapter.toString());
                else
                    bookVo.setNowChapter("");
            } else {
                bookVo.setNowChapter("");
            }
            resultVo.setData(bookVo);
        } else {
            resultVo.setCode(1L);
        }
        return resultVo;
    }

    @GetMapping("/book/chapter/{chapter}")
    public ResultVo getBookContent(@PathVariable("chapter") Integer chapter) {
        ContentVo bookContent = novelService.getBookContent(chapter);
        ResultVo resultVo = new ResultVo();
        if (bookContent != null) {
            resultVo.setData(bookContent);
        } else {
            resultVo.setCode(1L);
        }
        return resultVo;
    }

    @GetMapping("/myBookCase/{id}")
    public ResultVo getUserBooksById(@PathVariable("id") Integer id) {
        ResultVo resultVo = new ResultVo();
        List<NovelEntity> books = novelService.getBooksByUserId(id);
        if (books != null) {
            resultVo.setData(books);
        } else {
            resultVo.setCode(1L);
        }
        return resultVo;
    }

    @GetMapping("/book/hot/add/{id}")
    private void hotAdd(@PathVariable("id") String id, @CookieValue(name = "USERSESSION", defaultValue = "") String cookie) {
        if (!StringUtils.isEmpty(cookie)) {
            novelService.hotAddById(id);
        }
    }
}
